package uit.edu.vn.Activity;

import java.util.ArrayList;
import java.util.List;

import uit.edu.vn.Model.mySMS;
import uit.edu.vn.Utility.SMSListAdapter;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.res.Resources.Theme;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

public class SMSBackupActivity extends ListActivity{
	private Cursor myCursor;

    private ContentResolver cr; 
	private ListView listSMS;
	private List<mySMS> listSms = new ArrayList<mySMS>();
	private ArrayList<Boolean> itemSMSCheck=new  ArrayList<Boolean>(); 
	private ArrayList<String> itemSMSString= new ArrayList<String>();	
	private String columns[] = new String[] { People.NAME, People.NUMBER };
	
	private Handler handler;
	private ProgressDialog loadingImageDialog;
	private SMSListAdapter mAdapter ;
    private Uri mInboxURI;
    private String smsName ;
    private String smsBody ;
    
    private final int menu_Logout=0;
    private final int menu_Backup=1;
    private final int menu_MarkAll=2;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.backup_smsview);
	
		listSMS =getListView();
	    listSMS.setChoiceMode(2);

		 cr=getContentResolver();
		 mInboxURI= Uri.parse("content://sms/inbox"); 	
	    //goi Dialog
	    loadingImageDialog=ProgressDialog.show(SMSBackupActivity.this, "", "Đang tải dữ liệu ...",true);
	    //goi thread chay ngam voi UI
	    handler = new Handler();
	    Thread mySMSThread= new Thread(null,BackGroundThread,"BackGround");
	    mySMSThread.start();
	}
	//ham xu ly thread
	private Runnable BackGroundThread = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			doInBackGround();
		}
	};
	private void doInBackGround()
	{
		 myCursor=cr.query(mInboxURI, null, null, null, null);
		 startManagingCursor(myCursor);
		 while(myCursor.moveToNext())
	        {
	      		String tempNumber=null;
	        	tempNumber = myCursor.getString(myCursor.getColumnIndexOrThrow("address"));
	            smsBody =myCursor.getString(myCursor.getColumnIndexOrThrow("body"));
	            //goi ham tim kiem name;
	            smsName=lookingContact(tempNumber);
	            if(smsName==null)
	            {
	            	smsName=tempNumber;
	            }
	        	listSms.add(new mySMS(smsName, smsBody));
	        }	     
		 handler.post(doUpdate);
	}
	// cap nhat ui
	private Runnable doUpdate = new Runnable() 
	{	
		@Override
		public void run() {
			// TODO Auto-generated method stub
			doUpdateUI();
		}
	};
	private void doUpdateUI()
	{
		 mAdapter= new SMSListAdapter(this,listSms);
	     this.setListAdapter(mAdapter); 
	     // lay trang thai cac checkbox
	     itemSMSCheck=mAdapter.getCheckList();
	     //unload dialog
	     loadingImageDialog.dismiss();
	     
	}
	//cac ham lien quan khac
	public String lookingContact(String number)
	{
		Uri contactURI =Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, number);
		String nameContact=null;
		Cursor contactCuror = cr.query(contactURI, columns, null, null, null);
		if(contactCuror.moveToFirst())
		{
			nameContact=contactCuror.getString(contactCuror.getColumnIndex(People.NAME));
			
		}
		contactCuror.close();
		return nameContact;
	}
	public void showToast(String message)
    {
		Toast.makeText(this,message , Toast.LENGTH_SHORT).show();
    }
	// xu ly option menu 
	public boolean onCreateOptionsMenu(Menu menu) 
	{
	    	// TODO Auto-generated method stub
	    	//return super.onCreateOptionsMenu(menu);
	    	 menu.add(0, menu_Logout, 3,"Đăng Xuất").setIcon(R.drawable.ic_delete);
	    	 menu.add(0, menu_Backup, 2,"Sao Lưu").setIcon(R.drawable.ic_backup_select);
	    	 menu.add(0, menu_MarkAll,1, "Chon Hết").setIcon(R.drawable.ic_checkall);
			return true;
	    }
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	    	// TODO Auto-generated method stub
	    	switch(item.getItemId())
	    	{
	    		case menu_Backup:
	    			//xu ly backup
	    			// xoa mang item sms 
	    			itemSMSString.clear();
	    			String path= null;
	    			for(int i=0;i<itemSMSCheck.size();i++)
	    			{
	    				// kiem tra mang item check , neu la
	    				//true thi lay vi tri va truyen vao 
	    				//cursor
	    				if(itemSMSCheck.get(i)==true)
	    				{
	    	    	        int index = myCursor.getColumnIndexOrThrow("body");   	        
	    	    	        myCursor.moveToPosition(i);
	    	    	        path=myCursor.getString(index);
	    	    	        //add vao mang itemSMSString
	    					itemSMSString.add(path);
	    					Log.d("Chuoi item", path); 
	    					showToast(path);
	    				}
	    			}
	    			return true;
	    		case menu_Logout:
	    			//xu ly logout
	    			finish();
	    			return true;
	    		case menu_MarkAll:
	    			//xu ly check all
	    			for(int i=0;i<itemSMSCheck.size();i++)
	    		       {
	    				  itemSMSCheck.set(i, true);
	    		    	  mAdapter.setCheckList(itemSMSCheck);
	    		    	  
	    		       }
	    			this.setListAdapter(mAdapter);
	    			return true;
	    	}
	    	return false;
	    }
}
